package chances.epg.data.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import chances.epg.commons.utils.PathUtils;
import chances.epg.commons.utils.SpringWebUtils;
import chances.epg.config.EpgMvcConfig;

@SuppressWarnings("serial")
public class SearchUrlTag extends TagSupport {
	private String action;
	private String condition;
	private String type;
	private String size;
	private String name;
	private String code; //移除已有内容CODE
	@Override
	public int doEndTag() throws JspException {
		EpgMvcConfig config = SpringWebUtils.findBean(pageContext, EpgMvcConfig.class);
		String path = config.getSearchApiUrl();
		path = PathUtils.join(pageContext.getServletContext().getContextPath(),String.format(path, name));
		StringBuffer buffer = new StringBuffer(path).append("?action=").append(action);
		if(StringUtils.isNotEmpty(condition)){
			buffer.append("&condition=").append(this.condition);
		}
		if(StringUtils.isNotEmpty(type)){
			buffer.append("&type=").append(this.type);
		}
		if(StringUtils.isNotEmpty(size)){
			buffer.append("&size=").append(this.size);
		}
		if(StringUtils.isNotEmpty(code)){
			buffer.append("&code=").append(this.code);
		}
		this.release();
		pageContext.setAttribute(this.var, buffer.toString());
		return super.doEndTag();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	private String var;
	@Override
	public void release() {
		super.release();
		this.action = null;
		this.condition = null;
		this.type = null;
		this.size = null;
		this.code = null;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}


}
